﻿using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Runtime.InteropServices;

namespace Win32API
{
    public static class LocalPrinter
    {
        /// <summary>
        /// 系统所有打印机名称(默认将在第一)
        /// </summary> 
        public static IEnumerable<string> GetLocalPrinters()
        {
            var fPrinters = new List<string>();
            try
            {
                string s = "";
                using (var fPrintDocument = new PrintDocument())
                {
                    s = fPrintDocument.PrinterSettings.PrinterName;//默认打印机
                }
                if (s != "")
                {
                    //默认打印机始终出现在列表的第一项
                    fPrinters.Add(s);
                }
                foreach (string fPrinterName in PrinterSettings.InstalledPrinters)
                {
                    if (!fPrinters.Contains(fPrinterName))
                    {
                        fPrinters.Add(fPrinterName);
                    }
                }
            }
            catch
            { }
            return fPrinters;
        }
    }
}